import tkinter as tk
import tkinter.ttk
from tkinter import messagebox

import sqlite3
import os


class MainWindow(tk.Tk):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.title("成型加热确认表")
        self.geometry("640x450+180+80") # 窗口大小及位置
        self.resizable(0,0)
        # self["bg"] = "skyblue"  # 背景样式

        # 连接SqlLite数据库
        self.conn = sqlite3.connect('database.db')
        # 加载GUI
        self.setup_UI()

    def setup_UI(self):
        # 定义Label ：设定条件
        label_condition = tk.Label(self,
                     text='请选择设定条件：',  # 标签的文字
                     #bg='green',  # 标签背景颜色
                     font=('Arial', 12),  # 字体和字体大小
                     width=15, height=2  # 标签长宽(以字符长度计算)
                     )
        label_condition.grid(row=0,column=0)  # 固定窗口位置

        # 定义下拉框Combobox：用来存储设定条件
        cbox_condition = tkinter.ttk.Combobox(self)
        cbox_condition.grid(row=0,column=1)
        # 连接数据库获取下拉框值
        c = self.conn.cursor()
        cursor = c.execute("SELECT heating_time FROM heating_base")
        heating_time = []
        for row in cursor:  # 从数据库获取设定条件参数
            # print("heating_time:", row[0])
            heating_time.append(row[0])
        heating_time = list(set(heating_time))  # 使用内置函数set去除列表中的重复值
        cbox_condition['value']=heating_time # 下拉框的值
        cbox_condition.current(0)   # 下拉框的默认值

        # 定义Label：品番
        label_pinfan = tk.Label(self,text='品番',font=('Arial', 12),width=15, height=2)
        label_pinfan.grid(row=1,column=1)
        # 定义Label：批号
        label_batch = tk.Label(self,text='批号',font=('Arial', 12),width=15, height=2)
        label_batch.grid(row=1,column=2)
        # 定义Label：数量
        label_quantity = tk.Label(self,text='数量',font=('Arial', 12),width=15, height=2)
        label_quantity.grid(row=1,column=3)
        # 定义8个标签Label：存储1-8序号
        label_serial_number1 = tk.Label(self,text='1',font=('Arial', 12),width=4, height=1)
        label_serial_number1.grid(row=2,column=0)
        label_serial_number2 = tk.Label(self,text='2',font=('Arial', 12),width=4, height=1)
        label_serial_number2.grid(row=3,column=0)
        label_serial_number3 = tk.Label(self,text='3',font=('Arial', 12),width=4, height=1)
        label_serial_number3.grid(row=4,column=0)
        label_serial_number4 = tk.Label(self,text='4',font=('Arial', 12),width=4, height=1)
        label_serial_number4.grid(row=5,column=0)
        label_serial_number5 = tk.Label(self,text='5',font=('Arial', 12),width=4, height=1)
        label_serial_number5.grid(row=6,column=0)
        label_serial_number6 = tk.Label(self,text='6',font=('Arial', 12),width=4, height=1)
        label_serial_number6.grid(row=7,column=0)
        label_serial_number7 = tk.Label(self,text='7',font=('Arial', 12),width=4, height=1)
        label_serial_number7.grid(row=8,column=0)
        label_serial_number8 = tk.Label(self,text='8',font=('Arial', 12),width=4, height=1)
        label_serial_number8.grid(row=9,column=0)
        # 定义8个下拉框ComboBox：选择品番
        ## 连接数据库获取下拉框值
        c = self.conn.cursor()
        cursor = c.execute("SELECT heating_time FROM heating_base")
        print(cbox_condition.get())
        cbox_pinfan1 = tkinter.ttk.Combobox(self)
        cbox_pinfan1.grid(row=2,column=1)
        cbox_pinfan2 = tkinter.ttk.Combobox(self)
        cbox_pinfan2.grid(row=3,column=1)
        cbox_pinfan3 = tkinter.ttk.Combobox(self)
        cbox_pinfan3.grid(row=4,column=1)
        cbox_pinfan4 = tkinter.ttk.Combobox(self)
        cbox_pinfan4.grid(row=5,column=1)
        cbox_pinfan5 = tkinter.ttk.Combobox(self)
        cbox_pinfan5.grid(row=6,column=1)
        cbox_pinfan6 = tkinter.ttk.Combobox(self)
        cbox_pinfan6.grid(row=7,column=1)
        cbox_pinfan7 = tkinter.ttk.Combobox(self)
        cbox_pinfan7.grid(row=8,column=1)
        cbox_pinfan8 = tkinter.ttk.Combobox(self)
        cbox_pinfan8.grid(row=9,column=1)
        # 定义16个输入框Entry:用来对批号、数量进入输入
        ## 批号
        entry_batch1 = tk.Entry(self)
        entry_batch1.grid(row=2,column=2)
        entry_batch2 = tk.Entry(self)
        entry_batch2.grid(row=3,column=2)
        entry_batch3 = tk.Entry(self)
        entry_batch3.grid(row=4,column=2)
        entry_batch4 = tk.Entry(self)
        entry_batch4.grid(row=5,column=2)
        entry_batch5 = tk.Entry(self)
        entry_batch5.grid(row=6,column=2)
        entry_batch6 = tk.Entry(self)
        entry_batch6.grid(row=7,column=2)
        entry_batch7 = tk.Entry(self)
        entry_batch7.grid(row=8,column=2)
        entry_batch8 = tk.Entry(self)
        entry_batch8.grid(row=9,column=2)
        ## 数量
        entry_quantity1 = tk.Entry(self)
        entry_quantity1.grid(row=2,column=3)
        entry_quantity2 = tk.Entry(self)
        entry_quantity2.grid(row=3,column=3)
        entry_quantity3 = tk.Entry(self)
        entry_quantity3.grid(row=4,column=3)
        entry_quantity4 = tk.Entry(self)
        entry_quantity4.grid(row=5,column=3)
        entry_quantity5 = tk.Entry(self)
        entry_quantity5.grid(row=6,column=3)
        entry_quantity6 = tk.Entry(self)
        entry_quantity6.grid(row=7,column=3)
        entry_quantity7 = tk.Entry(self)
        entry_quantity7.grid(row=8,column=3)
        entry_quantity8 = tk.Entry(self)
        entry_quantity8.grid(row=9,column=3)
        # 定义Label：炉号
        label_heat_number = tk.Label(self,text='炉号：',font=('Arial', 12),width=15, height=2)
        label_heat_number.grid(row=10,column=0)
        # 定义输入框Entry：用来接收对炉号的输入
        entry_heat_number = tk.Entry(self)
        entry_heat_number.grid(row=10,column=1)
        # 定义Label：升温时间基准
        label_time_base = tk.Label(self,text='升温时间基准：',font=('Arial', 12),width=15, height=2)
        label_time_base.grid(row=11,column=0)
        # 定义下拉框ComboBox：用来选择“升温时间基准”
        cbox_time_base = tkinter.ttk.Combobox(self)
        cbox_time_base.grid(row=11,column=1)

        # 当按钮被点击的时候执行click_button()函数
        def click_button():
            # 使用消息对话框控件，showinfo()表示温馨提示
            messagebox.showinfo(title='温馨提示', message='欢迎使用C语言中文网')

        # 定义一个按钮：生成
        button_create = tk.Button(self,
                                  text='生成',
                                  bg='#7CCD7C',
                                  font=('Arial', 12),
                                  width=12, height=2,
                                  command=click_button)
        button_create.grid(row=12,column=2)

        self.conn.close()


if __name__ == '__main__':
    this_main = MainWindow()
    this_main.mainloop()
